#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "byte_handler.h"


char *removeZero(char *src, int size)
{
	int i = 0;
	char *buffer = malloc(size);

	for (i; i < size; i++)
	{
		if (src[i] == '\0')
			buffer[i] = '.';
		else
			buffer[i] = src[i];
	}

	return buffer;
}


void RemoveNewLine(char *src)
{
	int len = strlen(src);

	int i;

	for (i=0; i < len;i++)
	{
		if (src[i]=='\n')
		{	
			src[i]='\0';
		}
	}
	
}

char AsciiByteCompare(char *src, char *target)
{
	int i;

	int lengthSrc;
	int lengthTar;

	for (i=0;src[i]!='\0';i++) lengthSrc = i;
	for (i=0;target[i]!='\0'; i++) lengthTar = i;


	printf("Length: %d \t Length %d \n", lengthSrc, lengthTar);
	printf("String 1:\n %s",src);
	printf("String 2:\n %s", target);

	if (lengthSrc != lengthTar)
		return 0;

	for (i=0;src[i]==target[i];i++);

	if (i==lengthSrc)
		return 1;
}
 

char *GetFile(char filename[255], char *buffer, int length)
{	
	char in_filename[255];
	char out_filename[255];

	// In & out file descriptors
	int in_fileD;
	int out_fileD;

		// Add arguments to variables
		memset(&in_filename, 0, sizeof(in_filename));
		memset(&out_filename, 0, sizeof(out_filename));
		int len;

		len = strlen(in_filename);
		in_filename[len] = '\0';
		
		len = strlen(out_filename);
		out_filename[len] = '\0';

		strcpy(in_filename, filename);
		// strcpy(out_filename, argv[2]);




	// Start up file connection..
	if ( (in_fileD =  open(in_filename, O_RDONLY)) == EOF)	
	{
		printf("Failed to create file descriptor from input file\n");
		return NULL;
	}


	// Now it is time to examine the given file byte for byte
	// We will do this in packages of 512 bytes
	const int pack_size = 1;			// Bytes
	const int max_bytes = 10*1000;			// 10x1000 Bytes

	char byte_package[pack_size];
	int packages = 0;
	char *byte_buffer = (char *) malloc(20*1000);	// Reserve 20x1000 bytes memory
	ssize_t bytes_read;

	int mem_pos = 0;

	while ((bytes_read = read(in_fileD, byte_package, pack_size)) != 0)
	{
		char c = (char)* byte_package;
		printf("%x", c);
		byte_buffer[packages] = (int)c;
		packages++;
	}

	return byte_buffer;	

}

